package org.jeecg.modules.smartSchoolliuixao.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.smartSchoolStudentLeave.entity.SmartSchoolStudentLeave;
import org.jeecg.modules.smartSchoolliuixao.entity.SmartSchoolJiaqiliuxiao;
import org.jeecg.modules.smartSchoolliuixao.mapper.SmartSchoolJiaqiliuxiaoMapper;
import org.jeecg.modules.smartSchoolliuixao.service.ISmartSchoolJiaqiliuxiaoService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 学生假期留校
 * @Author: jeecg-boot
 * @Date:   2022-06-04
 * @Version: V1.0
 */
@Service
public class SmartSchoolJiaqiliuxiaoServiceImpl extends ServiceImpl<SmartSchoolJiaqiliuxiaoMapper, SmartSchoolJiaqiliuxiao> implements ISmartSchoolJiaqiliuxiaoService {

    @Override
    public void saveOrUpdateInfo(JSONArray form_component_values, String originator_userid) {
        {
            SmartSchoolJiaqiliuxiao student = new SmartSchoolJiaqiliuxiao();
            for (int i = 0; i < form_component_values.size(); i++) {
                JSONObject jsonObject = form_component_values.getJSONObject(i);
                String name = jsonObject.getString("name");
                String value = jsonObject.getString("value");
                switch (name){
                    case "姓名":
                        student.setStudenName(value);
                        break;
                    case "学号":
                        student.setStudentNum(value);
                        break;
                    case "本人电话":
                        student.setStudentNumber(value);
                        break;
                    case "现寝室号":
                        student.setDormitory(value);
                        break;
                    case "监护人姓名":
                        student.setParent(value);
                        break;
                    case "监护人电话":
                        student.setParentNumber(value);
                        break;
                    case "留校住宿事由":
                        student.setCause(value);
                        break;
                }
            }
            student.setId(originator_userid);
            List<SmartSchoolJiaqiliuxiao> diyStudents = baseMapper.selectList(new LambdaQueryWrapper<SmartSchoolJiaqiliuxiao>().eq(SmartSchoolJiaqiliuxiao::getId, student.getId()));
            if (diyStudents!=null && diyStudents.size()>0){
                baseMapper.update(student,new LambdaQueryWrapper<SmartSchoolJiaqiliuxiao>().eq(SmartSchoolJiaqiliuxiao::getId,student.getId()));
            }else {
                baseMapper.insert(student);
            }
        }
    }
}
